package com.alibaba.cloud.ai.example.helloworld.tools;

import cn.hutool.json.JSONUtil;
import com.alibaba.cloud.ai.example.flight.model.Flight;
import com.alibaba.cloud.ai.example.flight.service.FlightService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 * 测试封装一个专业版的【TOOL】
 * - 可以添加【样例提示描述信息】
 */
@Configuration
public class FlightCallTool {

    private FlightService flightService;

    public FlightCallTool(FlightService flightService) {
        this.flightService = flightService;
    }

    @Tool(description = "查询航班详情")
    public String queryFlightDetail(@ToolParam(description = "乘客名称") String name, @ToolParam(description = "订单号") Integer orderId) {
        Flight flight = flightService.queryFlight(name, orderId);
        if (Objects.nonNull(flight)) {
            return JSONUtil.toJsonStr(flight);
        }
        return Strings.EMPTY;
    }

    @Tool(description = "查询航班列表")
    public String queryFlightList() {
        List<Flight> flights = flightService.queryFilghtList();
        if (!CollectionUtils.isEmpty(flights)) {
            return JSONUtil.toJsonStr(flights);
        }
        return Strings.EMPTY;
    }
}
